package com.cabrida.livestar.register;

import org.apache.http.Header;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.login.LoginActivity;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.mainstar.MainStarActivity;
import com.cabrida.livestar.util.ComAnalysis;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.util.StringUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class RegisterActivity extends BaseActivity {
	public static final String TAG = RegisterActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private EditText et_mobile;
	private EditText et_psw;
	private EditText et_psw_again;
	private EditText et_code;
	private TextView tv_get_code;
	private Button btn_rigster;
	private boolean isLookPsw=false;
	private boolean isLookAgainPsw=false;
	private ImageButton ib_psw_look;
	private ImageButton ib_psw_again_look;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_register);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		et_mobile = (EditText) findViewById(R.id.et_mobile);
		et_psw = (EditText) findViewById(R.id.et_psw);
		et_psw_again = (EditText) findViewById(R.id.et_psw_again);
		et_code = (EditText) findViewById(R.id.et_code);
		tv_get_code = (TextView) findViewById(R.id.tv_get_code);
		btn_rigster = (Button) findViewById(R.id.btn_rigster);
		ib_psw_look = (ImageButton) findViewById(R.id.ib_psw_look);
		ib_psw_again_look = (ImageButton) findViewById(R.id.ib_psw_again_look);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		tv_title.setText("注册");
		iv_back.setOnClickListener(this);
		btn_rigster.setOnClickListener(this);
		tv_get_code.setOnClickListener(this);
		ib_psw_look.setOnClickListener(this);
		ib_psw_again_look.setOnClickListener(this);
	}

	/**
	 * 获取验证码 点击事件
	 */
	private void getCodeData() {
		String str_mobile = et_mobile.getText().toString().trim();
		if (TextUtils.isEmpty(str_mobile)) {
			showToast("请填写手机号");
			return;
		}
		if (!StringUtils.isPhone(str_mobile)) {
			showToast("请输入正确的手机号");
			return;
		}
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}

		showProgress();
		RequestParams params = new RequestParams();
		params.put("phone", str_mobile);
		 HttpUtils.getClient().post(Urls.GET_VERIFICATION_CODE_URL, params,
		 new JsonHttpResponseHandler() {
		
		 @Override
		 public void onSuccess(int statusCode, Header[] headers,
		 JSONObject response) {
		 dismissProgress();
		 L.e(TAG, "get_code_response", response.toString());
		 //"data":{"message":"验证码已下发，请注意查收"}
			JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());

			int code = JsonUtils.getInt(response, "code", -1);
			String message = JsonUtils.getString(data, "message", "");
		 
		 if (code == HttpHelp.Succeed) {
			 showToast(message);
		 } else {
			 showToast(message);
		 }
		
		 }
		
		 @Override
		 public void onFailure(int statusCode, Header[] headers,
		 String responseString, Throwable throwable) {
		 dismissProgress();
		 showToast(getResources().getString(R.string.fwq_error));
		 }
		 });
	}

	private void getRegisterData(String str_mobile, String str_password,String str_code) {

		RequestParams params = new RequestParams();
		params.put("phone", str_mobile);
		params.put("password", str_password);
		params.put("code", str_code);

		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.USER_REGISTER_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "register_response", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						
						if (code == HttpHelp.Succeed) {
							ComAnalysis.analysisJSONUser(context,response);
							showToast("注册成功！");
							 if (Global.U_type.equals(Global.USER_TYPE_STAR))
							 {
							 openActivity(MainStarActivity.class);
							 }else {
							 openActivity(MainActivity.class);
							 }
							finish();

						
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	/**
	 * 注册按钮 点击事件
	 */
	private void btn_register_click() {
		String str_mobile = et_mobile.getText().toString().trim();
		String str_password = et_psw.getText().toString().trim();
		String str_password_again = et_psw_again.getText().toString().trim();
		String str_code = et_code.getText().toString().trim();
		if (TextUtils.isEmpty(str_mobile)) {
			showToast("请填写手机号");
			return;
		}
		if (!StringUtils.isPhone(str_mobile)) {
			showToast("请输入正确的手机号");
			return;
		}
		
		if (TextUtils.isEmpty(str_code)||str_code.length()!=4) {
			showToast("请输入正确的验证码");
			return;
		}

		if (TextUtils.isEmpty(str_password)) {
			showToast("请填写密码");
			return;
		}
		if (TextUtils.isEmpty(str_password_again)) {
			showToast("请填写确认密码");
			return;
		}
		if (!str_password.equals(str_password_again)) {
			showToast("两次密码不同");
			return;
		}

		getRegisterData(str_mobile, str_password,str_code);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		openActivity(LoginActivity.class);
		this.finish();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			openActivity(LoginActivity.class);
			this.finish();
			break;
		case R.id.btn_rigster:
			btn_register_click();
			break;
		case R.id.tv_get_code:
			getCodeData();
			break;
		case R.id.ib_psw_look:
			if (isLookPsw) {
				et_psw.setTransformationMethod(PasswordTransformationMethod.getInstance());
				ib_psw_look.setBackgroundResource(R.drawable.login_look_no);
			}else {
				et_psw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				ib_psw_look.setBackgroundResource(R.drawable.login_look_yes);
			}
			isLookPsw=!isLookPsw;
			break;
		case R.id.ib_psw_again_look:
			if (isLookAgainPsw) {
				et_psw_again.setTransformationMethod(PasswordTransformationMethod.getInstance());
				ib_psw_again_look.setBackgroundResource(R.drawable.login_look_no);
			}else {
				et_psw_again.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				ib_psw_again_look.setBackgroundResource(R.drawable.login_look_yes);
			}
			isLookAgainPsw=!isLookAgainPsw;
			break;

		default:
			break;
		}
	}
}
